#ifndef __Tools_C
#define __Tools_C

#include "tools.h"


// 获取数组最大值:排除非0数;求平均值
uint16_t getArrayMax(uint16_t *arrDatas, uint8_t arrSize){
	  uint16_t max = 0;
	  uint8_t i = 0;	 
		for(i = 0; i < arrSize; i++){
			  if(arrDatas[i] > max && arrDatas[i] < CP_ADC_Limit){
					  max = arrDatas[i];
			  }
		}
		return max;
}

// 求平均值：去除最大值和最小值；
uint16_t getArrayAverage(uint16_t *arrDatas, uint8_t arrSize){
	  uint32_t total = 0;
	  uint8_t i = 0;
		for(i = 1; i < arrSize; i++){
			  total = total + arrDatas[i];
		}		
		return (uint16_t)total / arrSize;
}

// 求平方的和；
uint32_t getSquareSum(uint16_t *arrDatas, uint8_t arrSize){
	  uint32_t sum = 0;	  
	  uint8_t i = 0;
	  for(i = 0; i < arrSize; i++){
			  sum = sum + arrDatas[i] * arrDatas[i];
		}			
		return sum;
}

// 向数组中加入数字
void arrPostData(uint16_t *arrDatas, uint8_t arrSize,uint16_t addData){
	  uint8_t i = 0;
	  for(i = (arrSize - 1); i >= 1; i--){
			  arrDatas[i] = arrDatas[i - 1];
		}
		arrDatas[0] = addData;
}

// 向数组中加入数字:数组头部取数据
void arr8PostData(uint8_t *arrDatas, uint8_t arrSize,uint8_t addData){
	  uint8_t i = 0;
	  for(i = (arrSize - 1); i >= 1; i--){
			  arrDatas[i] = arrDatas[i - 1];
		}
		arrDatas[0] = addData;
}

//// 向数组中加入数字
//void arrCPPostData(uint8_t *arrDatas, uint8_t arrSize,uint16_t addData){
//	  uint8_t i = 0;
//	  for(i = (arrSize - 1); i >= 1; i--){
//			  arrDatas[i] = arrDatas[i - 1];
//		}
//		arrDatas[0] = addData;
//}

// 分析CP数据
//uint8_t analysis_cp_datas(uint8_t *arrDatas, uint8_t arrSize){
//	  uint8_t count_cp[4] = {0,0,0,0};
//		uint8_t max = 0;
//		uint8_t maxid = 0;
//	  uint8_t i = 0;
//		uint8_t cp_state_id_tmp = 0;
//	  for(i = 0; i < arrSize; i++){
//			  if(arrDatas[i] == CP_0){
//					   count_cp[0]++;
//				}else if(arrDatas[i] == CP_6){
//					   count_cp[1]++;
//				}else if(arrDatas[i] == CP_9){
//					   count_cp[2]++;
//				}else if(arrDatas[i] == CP_12){
//					   count_cp[3]++;
//				} else {
//				}
//		}
//		for(i = 1; i < 4; i++){
//			  if (arrDatas[i] > max) {
//            max = arrDatas[i];
//            maxid = i;
//        }
//		}
//		if(maxid == 1){
//				 cp_state_id_tmp = CP_6;
//		}else if(maxid == 2){
//				 cp_state_id_tmp = CP_9;
//		}else if(maxid == 3){
//				 cp_state_id_tmp = CP_12;
//		}
//		if(count_cp[0] > 0 && count_cp[1] == 0 && count_cp[2] == 0&& count_cp[3] == 0){
//			  cp_state_id_tmp = CP_0;
//		}
//		#ifdef SYS_DEBUG
//    //printf("count_cp = %d  %d  %d  %d", count_cp[0],count_cp[1],count_cp[2],count_cp[3]);	
//    #endif
//  	
//		return cp_state_id_tmp;
//}

// 分析获得错误码：低位先获得，低位优先级越高。错误码越低，优先级越高
uint8_t getFaultCode(uint16_t faultBitMask){	  
	  if(faultBitMask == 0) {
				return 0;
		} else {
				uint8_t i = 0;
				for(i = 0; i < 16; i++){
						if(faultBitMask & (0x0001 << i)){
							  return (i + 1);
						}
				}
		}
		return 0;
}

// 对数组进行集体赋值
void arrAllIs(uint8_t setValue,uint8_t *arrDatas, uint8_t arrSize){
	  uint8_t i = 0;
		for(i = 0; i < arrSize; i++){
				arrDatas[i] = setValue;
		}
}

// 延时函数：单位 = 50us; nus = 1 = 50us
void delayn50us(uint16_t nus){
    #ifdef AT32F403AVGT7	
	  uint16_t i = 400;
	  #else
	  uint8_t i = 200;
		#endif 
	  while(nus--){			  
			  while(i--);
		}
}
// 延时函数：单位 = 5us; nus = 1 = 5us
void delaynus(uint16_t nus){
	  #ifdef AT32F403AVGT7	
	  uint16_t i = 40;
	  #else
	  uint8_t i = 20;
		#endif 
	  while(nus--){			  
			  while(i--);
		}
}


#if card_type_sel > 0 || BLE_WIFY_EN > 0
void encryptStr(unsigned char *str,unsigned char *pwd,unsigned char csize){
		unsigned char i = 0;
		for(i = 0; i < csize; i++){    
			str[i] ^= pwd[i];
		}  
}

//判断两数组是否相等:size<256
bool isSameArr(unsigned char *arr1,unsigned char *arr2,unsigned char size){  
	  unsigned char i = 0;
		for(i = 0; i < size; i++){
				if(arr1[i] != arr2[i]){
						return false;      
				}
		}  
		return true;
}

#if BLE_WIFY_EN > 0
//判断两数组是否存在包含关系:size<256; 比较size个字符串;
int8_t isContainArr(unsigned char *arrContainer,unsigned char *arrBeContain,unsigned char ctnSize,unsigned char bectnSize){  
	  unsigned char i = 0, j = 0;
		for(i = 0; i < ctnSize; i++){
				if(arrContainer[i] == arrBeContain[0]){
					  for(j = 1; (j < bectnSize && (i + j) < ctnSize); j++){
					      if(arrContainer[i+j] != arrBeContain[j]){
									  i = i + j -1;
									  break;
								}
                if(j >= bectnSize -1){
									  return (i + j);
								}								
				    }
						
				}
		}  
		return -1;
}

// 将字符串追加到另一个字符串中：insertPosi = 插入位置。起始 = 0。
void appendArr(unsigned char *originArr,unsigned char *appendArr,unsigned char insertPosi,unsigned char appSize){
	  unsigned char i = 0;
	  for(i = 0; i < appSize; i++){
			  originArr[insertPosi + i] = appendArr[i];
		}
}

// 计算数组字符串长度：
uint8_t strLenMy(unsigned char *arr,unsigned char size){
	  unsigned char i = 0;
	  for(i = 0; i < size; i++){
			  if(arr[i] == '\0'){
					  return i;
				}
		}
		return size;
}

// 对数组进行判断:
bool isArrAllTheValue(uint8_t theValue,uint8_t *arrDatas, uint8_t arrSize){
	  uint8_t i = 0;
		for(i = 0; i < arrSize; i++){
			  if(arrDatas[i] != theValue){
					  return false;
				}
		}
		return true;
}

// 获取字符串中，某字符首次出现的位置
int8_t getCharPosiInArr(uint8_t theChar,uint8_t *arr, uint8_t arrSize){
	  uint8_t i = 0;
	  for(i = 0; i < arrSize; i++){
			  if(arr[i] == theChar){
					  return i;
				}
		}
		return -1;
}

// 16进制字符串转数字: 001 = 1; 023 = 23;
uint16_t strArrToHex(uint8_t *strArr, uint8_t Len){
	  uint16_t strInt = 0;
	  uint8_t i = 0;
    for(i = 0; i < Len; i++){
		    uint8_t bitNum = 0;
		    if(strArr[i] >= '0' && strArr[i] <= '9'){
				   bitNum = strArr[i] - '0';
			  }else if(strArr[i] >= 'A' && strArr[i] <= 'F'){
				   bitNum = (strArr[i] - 'A') + 10;
			  } else if(strArr[i] >= 'a' && strArr[i] <= 'f'){
				   bitNum = (strArr[i] - 'a') + 10;
			  }    
			  strInt = strInt + bitNum * intPow(16,(Len - i - 1)); 
		} 
	  return strInt; 
}

// 16进制字符串转hex整数;Len = 字符串字节数，即字符串个数。
void hexCharArrToHex(uint8_t *strArr, uint8_t Len,uint8_t *targetHexArr){
	  uint8_t i = 0;
	  uint8_t tmp[2];
	  if(Len > 1){
			  for(i = 0;i < Len; i = i + 2){
					  tmp[0] = strArr[i];
					  tmp[1] = strArr[i+1];
					  targetHexArr[i / 2] = (uint8_t)strArrToHex(tmp,2);
				}
		}	  
}

// 整数加解密算法：异或算法
void xw_endecode(uint8_t *input, uint8_t ilen, uint8_t *key, uint8_t keyLen, uint8_t *output){
	  int groupNum = ilen / keyLen + 1;
	  int i = 0;
	  int j = 0;
	  for(i = 0; i < groupNum; i++){
			  for(j = 0; j < keyLen; j++){
					   if(i * keyLen + j < ilen){							   
							   output[i * keyLen + j] = input[i * keyLen + j] ^ key[j];
						 }else{
							   return;
						 }
				}
		}
}


#endif

#endif

#if SEND_RTC_EN > 0
// 时间增加1秒 
//void tmrTimeAdd(int8_t *timerData,uint8_t dataLen){      
//	  if(dataLen > 0){ 
//			  timerData[0]++; 
//			  if(timerData[0] > 59){
//					  timerData[0] = 0x00;
//					  if(dataLen > 1){
//								timerData[1]++;
//								if(timerData[1] > 59){
//                    timerData[1] = 0x00;									
//									  if(dataLen > 2){
//												timerData[2]++;
//												if(timerData[2] > 24){
//														timerData[2] = 0x00;
//														if(dataLen > 3){
//																timerData[3]++;
//																if(timerData[3] > 59){
//																		timerData[3] = 0x00;
//																}
//														}
//												}
//										}
//								}
//						}
//		    }
//		}
//}

// Function to convert hexadecimal to BCD
uint8_t hex_to_bcd(uint8_t hex_value) {
	  uint8_t bcdValue;
    bcdValue = (hex_value / 10) << 4;
    bcdValue |= hex_value % 10;
	  return bcdValue;
}
 
// Function to convert BCD to hexadecimal
uint8_t bcd_to_hex(uint8_t bcd_value) {
    return ((bcd_value >> 4) * 10) + (bcd_value & 0x0F);
}

// 获取显示时间：
void getTmrShow(int8_t *timerData,uint8_t dataLen){	  
    uint32_t tmrTmp = (uint32_t)(system_time - lcdParam.lcd_pageStart_tm) / 1000;	
	  if(dataLen > 0){ 
			  timerData[0] = hex_to_bcd(tmrTmp % 60); 
				if(dataLen > 1){
						timerData[1] = hex_to_bcd((tmrTmp / 60) % 60);								                								
						if(dataLen > 2){
								timerData[2] = hex_to_bcd((tmrTmp / 3600));												
						}
				}		    
		}
		//printf(" %u  %u  %u  %f  %f \r\n",timerData[0],timerData[1],timerData[2],UI_Data.QOE,UI_Data.QOE_ChgStart);
}
#endif

// 计算校验和:包含start_idx和end_idx的元素，包含start_idx,不包含end_idx。
#if finger_sensor_type > 0
uint16_t getCheckSum_16Bit(uint8_t *dataArr,uint8_t start_idx,uint16_t end_idx){
	  uint16_t  i = 0;	 
    uint16_t checkSum = 0;	
	  for(i = start_idx; i < end_idx; i++){
			  checkSum = checkSum + dataArr[i];
		}
	  return checkSum;
}

#endif

#if CRC_Check  > 0
//CRC校验
uint8_t uchCRCHi;
uint8_t uchCRCLo;
static uint8_t auchCRCHi[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,0x81, 0x40, 0x00, 0x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 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,0x43, 0x83, 0x41, 0x81, 0x80, 0x40};


uint16_t CRC16(uint8_t * pMsg, uint16_t Len){   
	  uint8_t i; 
		uchCRCHi = 0xFF; 
		uchCRCLo = 0xFF; 
		while(Len--) {
				i = uchCRCHi ^ (*pMsg++); 
				uchCRCHi = uchCRCLo ^ auchCRCHi[i];
				uchCRCLo = auchCRCLo[i];
		}
		return (uchCRCHi << 8 | uchCRCLo);
}
#endif // #if CRC_Check  > 0

// 整数运算的幂函数:
uint16_t intPow(uint8_t base,uint8_t index){
	  uint16_t resultInt = 0;
	  uint8_t i = 0;
	  if(index <= 0){
			  resultInt = 1;
		}else{
			  resultInt = 1;
			  for(i = 0;i < index;i++){
					  resultInt = resultInt * base;
				}
		}
		return resultInt;
}

// 数字字符串转数字: 001 = 1; 023 = 23; 高位字符位权越大。  
uint16_t strArrToInt(uint8_t *strArr, uint8_t Len){
	  uint16_t strInt = 0;
	  uint8_t i = 0;
    for(i = 0; i < Len; i++){ 
			  uint8_t bitNum = strArr[i] - '0';  
			  if(bitNum > 9) bitNum = 0; 			  
			  strInt = strInt + bitNum * intPow(10,(Len - i - 1)); 
		} 
	  return strInt; 
}
#endif

